package cn.itcast.websocket.spring;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class MyHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {

        System.out.println("收到消息>>"+message.getPayload());

        //向客户端发送消息
        session.sendMessage(new TextMessage("消息已经收到"));

        if(message.getPayload().equals("10")){
            for(int i=0;i<10;i++){
                session.sendMessage(new TextMessage("消息->"+i));
                Thread.sleep( 100);
            }

        }
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        String o = (String) session.getAttributes().get("uid");
        session.sendMessage(new TextMessage(o+",欢迎你扑街"));

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("好走");
    }
}
